//
//  MYGPatientListViewController.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/4/18.
//

import UIKit
import JXSegmentedView

class MYGPatientListViewController: MYGBaseController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarShow()
        self.title = "患者管理"
       
        initUI()
    }
    @objc private func addAction(){
        self.popup.bottomSheet(with: true, bgColor: .black.withAlphaComponent(0.43)) {
            let actionSheet = MYGPatientAddView()
            actionSheet.selectIndex = 0
            actionSheet.dismissBlock = {
                self.popup.dismissPopup()
            }
            actionSheet.frame = CGRect.init(x: 0, y: 0, width: screenWidth , height: screenHeight)
            return actionSheet
        }
    }
    @objc func inviteAction(){
        self.popup.bottomSheet(with: true, bgColor: .black.withAlphaComponent(0.43)) {
            let actionSheet = MYGPatientAddView()
            actionSheet.selectIndex = 1
            actionSheet.dismissBlock = {
                self.popup.dismissPopup()
            }
            actionSheet.frame = CGRect.init(x: 0, y: 0, width: screenWidth , height: screenHeight)
            return actionSheet
        }
    }
    @objc func deleteAddView(){
        addView.removeFromSuperview()
    }
    @objc private func textValueChanged(){
        NotificationCenter.default.post(name: NSNotification.Name.init("patientScreen"), object: ["screenStr":searchTextField.text])
    }
    private func initUI(){
        
        view.addSubview(searchTextField)
        searchTextField.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(15))
            make.right.equalToSuperview().offset(-SCALEWIDTH(15))
            make.height.equalTo(SCALEWIDTH(35))
            make.top.equalToSuperview().offset(SCALEWIDTH(15))
        }
        view.addSubview(segmentedView)
        segmentedView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(SCALEWIDTH(45))
            make.top.equalTo(searchTextField.snp.bottom)
        }
        view.addSubview(listContainerView)
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-50)
            make.top.equalTo(segmentedView.snp.bottom)
        }
        //关联listContainer
        segmentedView.listContainer = listContainerView
        
        view.addSubview(addPBtn)
        addPBtn.snp.makeConstraints { make in
            make.left.bottom.equalToSuperview()
            make.width.equalTo(screenWidth/2)
            make.height.equalTo(50)
        }
        view.addSubview(invitePBtn)
        invitePBtn.snp.makeConstraints { make in
            make.right.bottom.equalToSuperview()
            make.width.equalTo(screenWidth/2)
            make.height.equalTo(50)
        }
        view.layoutIfNeeded()
        addPBtn.addCAGradientLayerColor(colors: [RGBA(r: 73, g: 117, b: 236, a: 1).cgColor,RGBA(r: 151, g: 179, b: 255, a: 1).cgColor], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 1, y: 0))
        invitePBtn.addCAGradientLayerColor(colors: [RGBA(r: 73, g: 117, b: 236, a: 1).cgColor,RGBA(r: 151, g: 179, b: 255, a: 1).cgColor], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 1, y: 0))
    }
    private lazy var searchTextField: UITextField = {
        let tf = UITextField()
        tf.font = .regular(14)
        tf.textColor = .rgba(51, 51, 51)
        tf.backgroundColor = .rgba(243, 243, 243, 1)
        tf.layer.masksToBounds = true
        tf.layer.cornerRadius = 15
        tf.attributedPlaceholder = NSMutableAttributedString.rz.colorfulConfer(confer: { confer in
            confer.image(UIImage(named: "Chat/search"))
            confer.text(" 搜索")?.font(.regular(14)).textColor(.rgba(142, 142, 147, 1))
        })
        tf.textAlignment = .center
        tf.addTarget(self, action: #selector(textValueChanged), for: .editingChanged)
        //tf.delegate = self
        return tf
    }()
    private lazy var listContainerView: JXSegmentedListContainerView = {
        let vi = JXSegmentedListContainerView(dataSource: self)
        return vi
    }()
    private lazy var segmentedView: JXSegmentedView = {
        let segmentedView = JXSegmentedView()
        segmentedView.delegate = self
        segmentedView.dataSource = dataSource
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = SCALEWIDTH(16)
        indicator.indicatorHeight = SCALEWIDTH(3)
        indicator.indicatorColor = .rgba(32, 92, 255, 1)
        segmentedView.indicators = [indicator]
        return segmentedView
    }()
    
    private lazy var dataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.titles = ["我的患者","我分享的","我收到的"]
        dataSource.titleNormalFont = .regular(13)
        dataSource.titleNormalColor = .rgba(118, 118, 118, 1)
        dataSource.titleSelectedFont = .regular(16)
        dataSource.titleSelectedColor = .rgba(32, 92, 255, 1)
        dataSource.isItemSpacingAverageEnabled = true
        return dataSource
    }()
    private lazy var addView:MYGPatientAddView = {
        let vi = MYGPatientAddView()
        return vi
    }()
    private lazy var addPBtn:UIButton = {
        let vi = UIButton()
        vi.setTitle("新增患者", for: .normal)
        vi.titleLabel?.font = .medium(17)
        vi.setTitleColor(.white, for: .normal)
        vi.addTarget(self, action: #selector(addAction), for: .touchUpInside)
        return vi
    }()
    private lazy var invitePBtn:UIButton = {
        let vi = UIButton()
        vi.setTitle("邀请患者", for: .normal)
        vi.titleLabel?.font = .medium(17)
        vi.setTitleColor(.white, for: .normal)
        vi.addTarget(self, action: #selector(inviteAction), for: .touchUpInside)
        return vi
    }()
}
extension MYGPatientListViewController: JXSegmentedViewDelegate{
    //点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，而不关心具体是点击还是滚动选中的情况。
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {}
    // 点击选中的情况才会调用该方法
    func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {}
    // 滚动选中的情况才会调用该方法
    func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {}
    // 正在滚动中的回调
    func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {}
}
extension MYGPatientListViewController:JXSegmentedListContainerViewDataSource{
    //返回列表的数量
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return 3
    }
    //返回遵从`JXSegmentedListContainerViewListDelegate`协议的实例
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let vc = MYGPatientListDetailViewController()
        vc.patientType = index + 1
        return vc
    }
    
}
